<?php
class LinkController extends Zend_Controller_Action
{	
    function init()
    {	
		$auth = Zend_Auth::getInstance();
		$this->initView();	
		if(!$auth->hasIdentity()){
			$this->_redirect('/');
		}
		Zend_Loader::loadclass('myacl');
        $this->view->baseUrl = $this->_request->getBaseUrl();
 		$this->view->title = 'Plima System';
		$this->view->user = (array)Zend_Auth::getInstance()->getIdentity();
		$this->db=Zend_Registry::get('dbAdapter');
		$this->select=$this->db->select();
		if(!empty($this->view->user)){			
			$this->view->role=$this->view->user['role'];
			$this->view->user_id=$this->view->user['u_id'];
			$this->view->username=$this->view->user['username'];
			$select=$this->db->select();
			$select->from('admin_user','*');			
			$select->join('plm_role','admin_user.role=plm_role.role_name','*');
			$select->where('admin_user.username = ?',$this->view->user['username']);
			$result=$this->db->fetchRow($select);
			$acl=unserialize($result['role_acl']);
			$this->view->acl=$acl;
			$this->view->myacl=new myacl($this->view->acl,$this->view->role);
		}
		$this->start=$this->_request->getParam("start");
		$limit=$this->_request->getParam("limit");
		$where=$this->_request->getParam("where");
		//是否开启了自动审核，即新增加的数据是否标记为已审核
		Zend_Loader::loadclass('adminconfig');
		$adminconfig=new adminconfig();	
		$is_autocheck=$adminconfig->fetchRow("config_path='admin/is_autocheck'");
		$this->is_autocheck=($is_autocheck['config_value']=="on")?"1":"0";
    }
 
   function indexAction() 
    { 	
		echo " ";exit;
    } 
 	
	function linkAction(){
		
		}
	
	function clientAction(){
		
		}
	
	function industryAction(){
		
		}
	function articleAction(){
		
		}
	function keywordAction(){
		
		}	
	function statisticAction(){
		if($this->_request->isPost()){
				$start=$this->_request->getParam("start");
				$limit=$this->_request->getParam("limit");
				$where=$this->_request->getParam("where");
				$type=$this->_request->getParam('type');
				$select=$this->select;
				$select->from('plm_link','*,count(link_pr) as pr_count');
				$select->join('plm_site','plm_link.link_site_id=plm_site.site_id','*');
				$select->join('plm_linktype','plm_link.link_linktype_id=plm_linktype.linktype_id','*');
				$select->join('plm_client','plm_link.link_client_id=plm_client.client_id','*');
				$select->join('plm_keyword','plm_keyword.keyword_id = plm_link.link_keyword_id');				
				if(!empty($where)){
					$select->where(stripslashes($where));
					}
				if(!$type){
					$select->order('link_id DESC');
				}
				//echo $select->__toString();
				$total=sizeof($this->db->fetchAll($select->__toString()));
				$select->limit($limit,$start);
				$data=$this->db->fetchAll($select->__toString());
				
				$result=array();
				if($type=='pr'){
					$select->group('link_pr');
					$select->order('link_pr DESC');	
					$data=$this->db->fetchAll($select->__toString());
					//print_r($data);
					for($i=0;$i<=10;$i++){
						$_temp=true;
					foreach($data as $item){
						if($item['link_pr']==$i){
							$result=array_merge($result,array('pr_'.$i=>$item['pr_count']));
							$_temp=false;
							}
						if($_temp){
							$result=array_merge($result,array('pr_'.$i=>0));
							}
							$result=array_merge($result,array('keyword'=>$item['keyword_name']));
							$result=array_merge($result,array('target_pr'=>$item['keyword_pr']));
						}
					}	
					//print_r($result);
					//获取总条数
					$db2=Zend_Registry::get('dbAdapter');
					$select_total=$db2->select();
					$select_total->from('plm_link','count(*) as total');
					if(!empty($where)){
					$select_total->where(stripslashes($where));
					}
					$total=$this->db->fetchAll($select_total->__toString());
					$total_all=$total[0]['total'];
					//获取存在条数
					$db3=Zend_Registry::get('dbAdapter');
					$select_exist=$db3->select();
					$select_exist->from('plm_link','count(*) as total');
					if(!empty($where)){
					$select_exist->where(stripslashes($where));
					}
					$select_exist->where('link_isexist is not NULL ');
					$total=$this->db->fetchAll($select_exist->__toString());
					$total_exist=$total[0]['total'];
					//print($total_exist);
					//获取收录条数
					$db4=Zend_Registry::get('dbAdapter');
					$select_google=$db4->select();
					$select_google->from('plm_link','count(*) as total');
					if(!empty($where)){
					$select_google->where(stripslashes($where));
					}
					$select_google->where('link_isgoogle is not NULL ');
					$total=$this->db->fetchAll($select_google->__toString());
					$total_google=$total[0]['total'];
					//echo $total_google;
					$result=array_merge($result,array('total_all'=>$total_all));
					$result=array_merge($result,array('total_exist'=>$total_exist));
					$result=array_merge($result,array('total_google'=>$total_google));
					$_temp=array();
					$_temp[]=$result;
					result(1,$_temp);
					exit;
				}
				echo '{"total":"'.$total.'","results":'.json_encode($data).',"message":'.$total;
				$select->where('link_isexist is not NULL ');
				$total=sizeof($this->db->fetchAll($select->__toString()));
				$select->where('link_isexist is ');
				echo ',"isexist":'.$total.'}';
				exit;
		}
	}
	function siteAction(){
		
		}
		
	function linktypeAction(){
		
		}
	
	function linkaddAction(){
		
		}
	
	function linkmoreAction(){
		
		}
	function keywordaddAction(){
		
		}
		
	function checkclientAction(){
		
		}
	function checkkeywordAction(){
		
		}
	function checklinkAction(){
		
		}	
	function linkinfoAction(){
		
		}
	
	function chartAction(){
				
		}
	//文件导入
	function importAction(){
		if($this->_request->isPost()){			
			Zend_Loader::loadclass('Custom_Csvfile');
			Zend_Loader::loadclass('keyword');
			Zend_Loader::loadclass('plmlink');		
			$keyword=new keyword();
			$plmlink=new plmlink();
			//print_r(file_get_contents($_FILES['import_file']['tmp_name']));
			$path=ROOT_DIR."/public/uploads/".date('m-d')."-".$this->view->username."-".$_FILES['import_file']['name'];
			if(move_uploaded_file($_FILES['import_file']['tmp_name'],iconv("UTF-8","gb2312",$path))){
			$csv=new Custom_Csvfile($path);	
				$i=0;
				$result=array();
				while($csv->next_Row()) {
					if(!$csv->f('Target_Page')){
						failure('未找到Target_Page和Inbound_Link，二者缺一不可');
						exit;						
						}					
					$keyword_link = trim($csv->f('Target_Page'));
					$link_address=trim($csv->f('Inbound_Link'));					
					$result[]=array('keyword_link'=>$keyword_link,'link_address'=>$link_address);
					$i++;
				}
			$j=$error_link_exist=$error_keyword_noexist=0;
			foreach($result as $item){				
				$link_address=$item['link_address'];
				$keyword_link=$item['keyword_link'];	
				$keyword_link_arr=explode(",",$keyword_link);
				foreach($keyword_link_arr as $keyword_link){
				
				$keyword_link_info=$keyword->fetchRow("keyword_link='$keyword_link'");
				$link_info=$plmlink->fetchRow("link_address='$link_address'");
				//如果该地址已存在,则不添加至数据库
				if($link_info){
					$error_link_exist++;
					continue;
					}
				//如果能找到关键词信息
				if($keyword_link_info){
					$link_info=array(
								'link_linktype_id'			=> $_POST['linktype_id'],
								'link_client_id'			=> $keyword_link_info['keyword_client_id'],
								'link_site_id'				=> $_POST['site_id'],
								'link_user_id'				=> $this->view->user_id,
								'link_keyword_id'			=> $keyword_link_info['keyword_id'],
								'link_price'				=> ($_POST['link_price']=="")?"0":$_POST['link_price'],
								'link_address'				=> $link_address,
								'link_input_date'			=> date('y-m-d H:i:s'),
								'link_ischeck'				=> $this->is_autocheck
									 );
					if($plmlink->insert($link_info)){
					$j++;	
						}					
				}
				else{
					$error_keyword_noexist++;
					}
				}
				}
			success("共有<font color='red'>".$i."</font>条数据被发现,<br />有<font color='red'>".$j."</font>条导入成功,<br />有<font color='red'>".$error_link_exist."</font>条Inbound Link已存在,<br />有<font color='red'>".$error_keyword_noexist."</font>个关键字找不到关键词信息，请核对。");
			//print_r($result);
			}
			else{
				failure("上传文件失败");
				}
			exit;
			}
		}
	
	function userdataAction(){
		Zend_Loader::loadclass('user');
		$user=new user();
		$data=$user->fetchAll("role='seo'")->toArray();
		result(count($data),$data);
		exit;
		}
		
	function autocheckAction(){
		Zend_Loader::loadclass('adminconfig');
		$adminconfig=new adminconfig();	
		$autocheck=$adminconfig->fetchRow("config_path='admin/autocheck'");
		$expireday=$adminconfig->fetchRow("config_path='admin/expireday'");
		$is_autocheck=$adminconfig->fetchRow("config_path='admin/is_autocheck'");
		$this->view->isautocheck=$autocheck['config_key'];
		$this->view->autochecktime=$autocheck['config_value'];
		$this->view->expireday=$expireday['config_value'];
		$this->view->is_autocheck=$is_autocheck['config_value'];		
		}
	function clientaddAction(){		
		}	
	function articleaddAction(){
		
		}
	function articlereferenceAction(){
		//引用软文
		Zend_Loader::loadclass('article');
		$article=new article();
		$id=intval($this->_request->getParam("id"));
		if(!$id){
			echo "没有软文ID信息，请确认";
			exit;
			}
		$info=$article->fetchRow('article_id='.$id);
		$this->view->id=$id;
		$this->view->article_title=$info['article_title'];
		$this->view->article_content=str_replace(array("\r","\n")," ",$info['article_content']);		
		}
		
	function articlelistAction(){
		
		}
	function progressAction(){
		Zend_Loader::loadclass('plmlink');
		$select=$this->db->select();
		$select->from('admin_config');
		$select->where("config_path='admin/autocheck'");	
		$data=$this->db->fetchRow($select->__toString());
		$interval=$data['config_value'];
		$plmlink=new plmlink();
		if($this->_request->isPost()){
			//检查链接是否存在
			Zend_Loader::loadclass('plmlink');
			Zend_Loader::loadClass('Mivec_Http_Socket');
			$plmlink=new plmlink();
			$data=$plmlink->fetchRow('link_isinfo is null ')->toArray();
			$key=$data['link_address'];
			$id=$data['link_id'];
			$_client = new Mivec_Http_Socket($key);
			$_status=$_client->request()->getStatus();
					if($_status=='404'){
					$info=array(
							'link_isexist' 		=> 0,
							'link_isinfo'		=> 1,					
							'link_lastupdate'	=> date('y-m-d H:i:s')
							);
					}
					if($_status=='200'){
					$info=array(
							'link_isexist' 		=> 1,
							'link_isinfo'		=> 1,					
							'link_lastupdate'	=> date('y-m-d H:i:s')
							);
					}
			$plmlink->update($info,'link_id='.$id);
			success("失败");
			exit;
			}
		$this->view->linkcount=count($plmlink->fetchAll("link_isinfo is null ")->toArray());
		
		$this->view->interval=$interval*1000;
		}
	function serverindustryAction(){
		$type=$this->_request->getParam("type");
		Zend_Loader::loadclass('industry');
		switch($type){
			//获取行业数据
			case "dataindustry":				
				$id=$this->_request->getParam("node");
				$industry=new industry();
				$data=$industry->getAll($id);
				echo $data;exit;
				break;
			case "addIndustry":
				$industry=new industry();
				$name=$_REQUEST['name'];
				$id=$_REQUEST['top_id'];
				$data=array(
							"industry_name"		=> $name,
							"industry_top_id"	=> $id
							);
				$industry->insert($data);
				success("OK");
				break;
			case "delIndustry":
				$industry=new industry();
				$id=$this->_request->getParam("id");
				$industry->delete("industry_id = $id");
				success("OK");
				break;
			case "editIndustry":
				$industry=new industry();
				$id=$this->_request->getParam("id");
				$name=$this->_request->getParam("name");
				$data=array(
							"industry_id"		=> $id,
							"industry_name"		=> $name
							);
				$industry->update($data,"industry_id = $id");
				success("OK");
				break;
			case "dragIndustry":
				$industry=new industry();
				$id=$this->_request->getParam("id");
				$new_top_id=$this->_request->getParam("new_top_id");
				$data=array(
							"industry_top_id"		=> $new_top_id
							);
				$industry->update($data,"industry_id = $id");
				success("OK");
				break;
			default:				
				$industry=new industry();
				$data=$industry->fetchAll(null,"industry_id")->toArray();
				result(sizeof($data),$data);
				exit;
			break;
			}
		exit;
		}
	
	
	function serverclientAction(){
		Zend_Loader::loadclass('client');
		$client=new client();
		$type=$this->_request->getParam("type");
		switch($type){
			//获取用户数据
			case "edit":				
				//echo $data=stripslashes($_POST['data']);				
				//var_dump(json_decode($data));
				$data=json_decode(stripslashes($_POST['data']));								
				foreach($data as $item){
					$info=array(
								'client_id'			=> $item->client_id,
								'client_company'	=> $item->client_company,
								'client_industry'	=> $item->client_industry,
								'client_host'		=> $item->client_host
								);
					if($client->edit($info)){
						success("OK");
						}
					else{
						failure("失败");
						}
				}				
			break;
			case "add":
				$client_company=$this->_request->getPost('client_company');
				$client_host=$this->_request->getPost('client_host');
				$client_industry=$this->_request->getPost('client_industry');
				$info=array(								
								'client_company'	=> $client_company,
								'client_industry'	=> $client_industry,
								'client_host'		=> $client_host,
								'client_ischeck'	=> $this->is_autocheck
								);
				print_r($info);
				if($client->insert($info)){
						success("OK");
						}
					else{
						failure("失败");
						}
			break;
			case "del":
				$ids=$this->_request->getPost('id');
				if($client->del($ids)){
					success($ids);
				}
				else{
				error("发生错误");
				}
			break;
			case "getIndusty":
			Zend_Loader::loadclass('industry');	
			$industry=new industry();
			$data=$industry->fetchAll(null,"industry_id")->toArray();
			result(sizeof($data),$data);
			break;
			case "getAll":
			Zend_Loader::loadclass('keyword');
			$keyword=new keyword();
			//print_r(get_class_methods(get_class($keyword)));exit;
			$start=$this->_request->getParam("start");
			$limit=$this->_request->getParam("limit");
			$total=count($client->fetchAll(null,"client_id")->toArray());
			$data=$client->fetchAll(null,"client_id",$limit,$start)->toArray();
			foreach($data as $item){				
				$client_id=$item['client_id'];
				$query="SELECT count(keyword_count) AS count from plm_keyword where keyword_client_id='$client_id'";
				$dbcom=$keyword->getAdapter();
				$_data=$dbcom->fetchRow($query);
				$_keyword=$keyword->fetchAll("keyword_client_id=".$client_id)->toArray();
				$_keyword_name_list=$_keyword_link_list=array();
				foreach($_keyword as $_item){
					$_keyword_name_list[]=$_item['keyword_name'];
					$_keyword_link_list[]=$_item['keyword_link'];
					}
				
				$client_keyword_name=implode('<br />',$_keyword_name_list);
				$client_keyword_link=implode('<br />',$_keyword_link_list);
				$count=$_data['count'];
				$_arrayCount=array('client_keyword_count'=>$count);
				$_arraykeyword_name=array('client_keyword'=>$client_keyword_name);
				$_arraykeyword_link=array('client_keyword_link'=>$client_keyword_link);
				$result[]=array_merge($item,$_arrayCount,$_arraykeyword_name,$_arraykeyword_link);				
				}
			result($total,$result);
			break;
			case "check":
				$ids=$this->_request->getPost('id');
				$flag=($this->_request->getPost('flag')=="true")?"1":"0";				
				$info=array("client_ischeck"=>$flag);
				if(is_numeric($ids)){
					$client->update($info,"client_id=".$ids);
					}
				else{
					$client->update($info,"client_id in ($ids)");
					}
			success("ok");
			break;	
			case "getcompany":
				$select=$this->select;
				$select->from('plm_client','*');
				$select->group('client_company');
				$total=sizeof($this->db->fetchAll($select->__toString()));
				$select->limit($limit,$start);
				$data=$this->db->fetchAll($select->__toString());
				result($total,$data);
			break;
			default:
				$client_host=$this->_request->getParam("client_host");
				if($client_host){
					$data=$client->fetchAll("client_host like '%".$client_host."%' and client_ischeck='1'","client_id")->toArray();	
					$total=sizeof($data);
					$data=$client->fetchAll("client_host like '%".$client_host."%'  and client_ischeck='1'","client_id",$limit,$start)->toArray();	
					result($total,$data);					
					}
				else{
				$where=$this->_request->getParam('where');
				$where=($where=="")?"":"and ".$where;
				$data=$client->fetchAll("client_ischeck='1' ".$where,"client_id")->toArray();				
				$total=sizeof($data);
				$data=$client->fetchAll("client_ischeck='1' ".$where,"client_id",$limit,$start)->toArray();				
				result($total,$data);
				}
			break;
		}
		exit;
	}
	function serverarticleAction(){
		Zend_Loader::loadclass('article');
		$article=new article();
		$type=$this->_request->getParam("type");
		switch($type){
			case "edit":
				$data=json_decode(stripslashes($_POST['data']));
				foreach($data as $item){					
					$info=array(								
								'article_title'		=> $item->article_title,								
								'article_content'	=> $item->article_content,
								'article_tags' 		=> $item->article_tags
								);				
					if($item->article_id){
						$id=$item->article_id;
						$article->update($info,"article_id= $id");
						success("OK");
						}
					else{
						$article->insert($info);
						success("OK");
						}
				}				
			break;
			case "add":
				$info=array(								
								'article_title'		=> $this->_request->getParam('article_title'),								
								'article_content'	=> $this->_request->getParam('article_content'),								
								'article_tags'		=> $this->_request->getParam('article_tags'),
								'article_ischeck'	=> $this->is_autocheck								
								);
				if($article->insert($info)){
					success("OK");
					}
				else{
					failure("失败");
					}
				break;
			case "del":
				$ids=$this->_request->getPost('id');
				if($article->del($ids)){
					success($ids);
				}
				else{
				error("发生错误");
				}
			break;
			case 'articlereferencelist':
				$select=$this->select;
				$select->from('plm_article','*');
				$select->join('plm_client','plm_article.article_client_id = plm_client.client_id',"*");
				$select->order('article_id DESC');
				if(!empty($where)){
					$select->where(stripslashes($where));
					}
				$total=sizeof($this->db->fetchAll($select->__toString()));
				$select->limit($limit,$start);
				$data=$this->db->fetchAll($select->__toString());				
				result($total,$data);
			break;
			default:
				$start=$this->_request->getParam("start");
				$limit=$this->_request->getParam("limit");
				
				$data=$article->fetchAll(null,"article_id",$limit,$start)->toArray();
				$total=sizeof($data);
				result($total,$data);
				exit;
			break;
		}
		exit;
		}
	function serverkeywordAction(){
		Zend_Loader::loadclass('keyword');
		$keyword=new keyword();
		$type=$this->_request->getParam("type");
		switch($type){
			case "edit":
				$data=json_decode(stripslashes($_POST['data']));
				foreach($data as $item){
					$info=array(
								'keyword_id'			=> $item->keyword_id,								
								'keyword_client_id'		=> $item->keyword_client_id,
								'keyword_name'			=> $item->keyword_name,
								'keyword_link'			=> $item->keyword_link
								);
					if($keyword->edit($info)){
						success("OK");
						}
					else{
						failure("失败");
						}
				}				
			break;
			case "add":
				$keyword_name=$this->_request->getParam("keyword_name");
				$keyword_host=$this->_request->getParam("client_host");
				Zend_Loader::loadclass('client');
				$client=new client();
				$keyword_client=$client->fetchRow("client_host='$keyword_host'");
				$keyword_client_id=$keyword_client['client_id'];
				//$keyword_client_id=$this->_request->getParam("client_id");
				$keyword_link=$this->_request->getParam("keyword_link");
				$keyword_link_decription=$this->_request->getParam("keyword_link_decription");
				for($_i=0;$_i<sizeof($keyword_link);$_i++){
					if(!empty($keyword_link[$_i])){
						$info=array(
									"keyword_name"		=> $keyword_name,
									"keyword_client_id"	=> $keyword_client_id,
									"keyword_host"		=> $keyword_host,
									"keyword_link"		=> $keyword_link[$_i],
									"keyword_link_decription"=> $keyword_link_decription[$_i],
									'keyword_ischeck'	=> $this->is_autocheck
									);
						$keyword->insert($info);						
						}
					}
				success("OK");
				break;
			case "del":
				$ids=$this->_request->getPost('id');
				if($keyword->del($ids)){
					success($ids);
				}
				else{
				error("发生错误");
				}
			break;
			case "check":
				$ids=$this->_request->getPost('id');
				$flag=($this->_request->getPost('flag')=="true")?"1":"0";				
				$info=array("keyword_ischeck"=>$flag);
				if(is_numeric($ids)){
					$keyword->update($info,"keyword_id=".$ids);
					}
				else{
					$keyword->update($info,"keyword_id in ($ids)");
					}
			success("ok");
			break;
			default:
				$start=$this->_request->getParam("start");
				$limit=$this->_request->getParam("limit");
				$client_id=$this->_request->getParam("client_id");
				$ischeck=$this->_request->getParam("ischeck");
				$where=stripslashes($this->_request->getParam("where"));
				if(empty($client_id)){									
					$data=$keyword->fetchAll(null,"keyword_id DESC")->toArray();
					$total=sizeof($data);
					$data=$keyword->fetchAll(null,"keyword_id DESC",$limit,$start)->toArray();
				}				
				else{
					$data=$keyword->fetchAll("keyword_client_id = $client_id and keyword_ischeck='1'","keyword_id DESC")->toArray();
					$total=sizeof($data);
					$data=$keyword->fetchAll("keyword_client_id = $client_id and keyword_ischeck='1'","keyword_id DESC",$limit,$start)->toArray();
					}
				if(!empty($where)){
					
					$data=$keyword->fetchAll($where,"keyword_id DESC")->toArray();
					$total=sizeof($data);
					$data=$keyword->fetchAll($where,"keyword_id DESC",$limit,$start)->toArray();
					}	
				result($total,$data);
				exit;
			break;
		}
		exit;
		}
	
	function serversiteAction(){
		Zend_Loader::loadclass('site');
		$site=new site();
		$type=$this->_request->getParam("type");
		switch($type){
			case "edit":
				$data=json_decode(stripslashes($_POST['data']));
				foreach($data as $item){
					$info=array(
								'site_id'				=> $item->site_id,								
								'site_type'				=> $item->site_type
								);
					if(is_numeric($item->site_id)){
					$site->update($info,'site_id='.$item->site_id);
					}
					else{
						$info=array(
									"site_type"			=> $item->site_type
									);
						$site->insert($info);
						}
				}				
			break;
			case "del":
				$ids=$this->_request->getPost('id');
				if($site->del($ids)){
					success($ids);
				}
				else{
				failure("发生错误");
				}
			break;
			default:
				$start=$this->_request->getParam("start");
				$limit=$this->_request->getParam("limit");
				$data=$site->fetchAll(null,"site_id")->toArray();
				$total=sizeof($data);
				result($total,$data);
				exit;
			break;
		}
		exit;
		}
	
	function serverlinktypeAction(){
		Zend_Loader::loadclass('linktype');
		$linktype=new linktype();
		$type=$this->_request->getParam("type");
		switch($type){
			case "edit":
				$data=json_decode(stripslashes($_POST['data']));
				foreach($data as $item){
					$info=array(
								'linktype_id'				=> $item->linktype_id,								
								'linktype_name'				=> $item->linktype_name
								);
					if(is_numeric($item->linktype_id)){
					$linktype->update($info,'linktype_id='.$item->linktype_id);
					}
					else{
						$info=array(
									"linktype_name"			=> $item->linktype_name
									);
						$linktype->insert($info);
						}
				}				
			break;
			case "del":
				$ids=$this->_request->getPost('id');
				if($linktype->del($ids)){
					success($ids);
				}
				else{
				error("发生错误");
				}
			break;			
			default:			
				$data=$linktype->fetchAll(null,"linktype_id")->toArray();
				$total=sizeof($data);
				result($total,$data);
				exit;
			break;
		}
		exit;
		}
	
	function serverlinkAction(){
		Zend_Loader::loadclass('plmlink');
		$plmlink=new plmlink();
		$type=$this->_request->getParam("type");
		switch($type){
			case "edit":
				$data=json_decode(stripslashes($_POST['data']));
				foreach($data as $item){
					$info=array(
								'link_linktype_id'			=> $item->linktype_id,
								'link_client_id'			=> $item->client_id,
								'link_site_id'				=> $item->site_id,
								'link_keyword_id'			=> $item->link_keyword_id,
								'link_address'				=> $item->link_address
								);
					if(is_numeric($item->link_id)){
					$plmlink->update($info,'link_id='.$item->link_id);
					}
					else{
						$plmlink->insert($info);
						}
				}				
			break;
			case "add":		
				$_array=explode(",",$_POST['link_keyword_id']);
				Zend_Loader::loadclass('keyword');
				$keyword=new keyword();
				foreach($_array as $item){
						$_info=array(
									 'keyword_count'	=> new Zend_Db_Expr('keyword_count+1')
									 );
						$keyword->update($_info,"keyword_name='$item'");
						$_linkinfo=array(
								'link_linktype_id'			=> $_POST['linktype_id'],
								'link_client_id'			=> $_POST['client_id'],
								'link_site_id'				=> $_POST['site_id'],
								'link_user_id'				=> $_POST['user_id'],
								'link_keyword_id'			=> $item,
								'link_price'				=> ($_POST['link_price']=="")?"0":$_POST['link_price'],
								'link_address'				=> $_POST['link_address'],
								'link_input_date'			=> date('y-m-d H:i:s'),
								'link_ischeck'				=> $this->is_autocheck
								);						
						$plmlink->insert($_linkinfo);
					}								
				success("ok");
			break;
			//软文引用
			case 'addarticlereference':
				Zend_Loader::loadclass('article');
				$article=new article();
				$article_id=$this->_request->getparam('article_id');
				$info=array(
							'article_title'		=> $this->_request->getParam('article_title'),
							'article_content'	=> $this->_request->getParam('article_content'),
							'article_client_id' => $this->_request->getParam('client_id')							
							);
				$article_id=$article->isNew($article_id,$info);
				$_array=explode(",",$_POST['link_keyword_id']);
				Zend_Loader::loadclass('keyword');
				$keyword=new keyword();
				foreach($_array as $item){
						$_info=array(
									 'keyword_count'	=> new Zend_Db_Expr('keyword_count+1')
									 );
						$keyword->update($_info,"keyword_name='$item'");
						$_linkinfo=array(
								'link_linktype_id'			=> $_POST['linktype_id'],
								'link_article_id'			=> $article_id,
								'link_client_id'			=> $_POST['client_id'],
								'link_site_id'				=> $_POST['site_id'],
								'link_keyword_id'			=> $item,
								'link_address'				=> $_POST['link_address'],
								'link_input_date'			=> date('y-m-d H:i:s')
								);
						$plmlink->insert($_linkinfo);
					}
					success("ok");
			break;
			case "del":
				$ids=$this->_request->getPost('id');
				if($plmlink->del($ids)){
					success($ids);
				}
				else{
				error("发生错误");
				}
			break;
			case "check":
				$ids=$this->_request->getPost('id');
				$flag=($this->_request->getPost('flag')=="true")?"1":"0";				
				$info=array("link_ischeck"=>$flag);
				if(is_numeric($ids)){
					$plmlink->update($info,"link_id=".$ids);
					}
				else{
					$plmlink->update($info,"link_id in ($ids)");
					}
			success("ok");
			break;
			default:
				$start=$this->_request->getParam("start");
				$limit=$this->_request->getParam("limit");
				$where=$this->_request->getParam("where");
				$select=$this->select;
				$select->from('plm_link','*');
				$select->join('plm_site','plm_link.link_site_id=plm_site.site_id','*');
				$select->join('plm_linktype','plm_link.link_linktype_id=plm_linktype.linktype_id','*');
				$select->join('plm_client','plm_link.link_client_id=plm_client.client_id','*');
				$select->join('plm_keyword','plm_keyword.keyword_id = plm_link.link_keyword_id');
				$select->order('link_id DESC');
				if(!empty($where)){
					$select->where(stripslashes($where));
					}
				//echo $select->__toString();
				$total=sizeof($this->db->fetchAll($select->__toString()));
				$select->limit($limit,$start);
				$data=$this->db->fetchAll($select->__toString());				
				result($total,$data);
				exit;
			break;
		}
		exit;
		}
	
	function serverconfigAction(){
		Zend_Loader::loadclass('adminconfig');
		$adminconfig=new adminconfig();		
		$type=$this->_request->getParam("type");
		switch($type){
		case "expire_day":
		$info=array(
					'config_key'	=> '0',
					'config_value'	=> $this->_request->getParam('expire_day'),
					'config_isactive'=>'1'
					);
		$adminconfig->update($info,"config_path='admin/expireday'");
		$info=array(
					'config_key'	=> '0',
					'config_value'	=> $this->_request->getParam('is_autocheck'),
					'config_isactive'=>'1'
					);
		$adminconfig->update($info,"config_path='admin/is_autocheck'");
		success("ok");
		break;
		default:
		$info=array(
					"config_key"	=> $this->_request->getParam('autocheck_type'),
					"config_value"	=> $this->_request->getParam('autocheck_time')
					);
		if($adminconfig->update($info,"config_path='admin/autocheck'")){
			success("OK");
			}
		else{
			failure("失败");
			}
		break;
		}
		exit;			
	}
	
	function autoAction(){	
	ignore_user_abort();
	set_time_limit(0);
	$select=$this->db->select();
	$select->from('admin_config');
	$select->where("config_path='admin/autocheck'");	
	$data=$this->db->fetchRow($select->__toString());
	if(count($data)>0 && $data['config_isactive']){		
		Zend_Loader::loadclass('Task_Linkcheck');
		$linkcheck=new Task_Linkcheck();		
		$linkcheck->start();
		}	
	success("OK");
	exit;
	}
	
	
	//检测
	function servercheckAction(){
		Zend_loader::loadclass("plmlink");
		$plmlink=new plmlink();
		$ids=$this->_request->getParam("id");
		$_array=explode(",",$ids);
		$type=$this->_request->getParam("type");
		switch($type){
			//检查是否存在
			case "exist":
				Zend_Loader::loadClass('Mivec_Http_Socket');
				foreach($_array as $id){
					$_temp=$plmlink->fetchRow('link_id='.$id);
					$key=$_temp['link_address'];
					$_client = new Mivec_Http_Socket($key);
					$_status=$_client->request()->getStatus();
					if($_status=='404'){
					$info=array(
							'link_isexist' 		=> 0,
							'link_isinfo'		=> 1,					
							'link_lastupdate'	=> date('y-m-d H:i:s')
							);
					}
					if($_status=='200'){
					$info=array(
							'link_isexist' 		=> 1,
							'link_isinfo'		=> 1,					
							'link_lastupdate'	=> date('y-m-d H:i:s')
							);
					}
					$plmlink->update($info,'link_id='.$id);
				}
				success("OK");
				break;
			//检查inboundlinkPR值	
			case "check":
				Zend_Loader::loadClass('Mivec_Http_Socket');
				Zend_Loader::loadClass('Http_Google_Data');
				Zend_Loader::loadClass('Http_Google_Pr');
				foreach($_array as $id){
					$_temp=$plmlink->fetchRow('link_id='.$id);
					$key=$_temp['link_address'];
					$_client = new Mivec_Http_Socket($key);
					$_status=$_client->request()->getStatus();
					$link_isexist=($_status=="200")?"1":"0";
					$normal = new Http_Google_Data($key);
					$_resultCount=$normal->getResultCount();
					if(_resultCount>0){
					$_pr = new Http_Google_Pr($key);	
					$pr=$_pr->getPR();
					$home_pr=$_pr->getHomePR();
					$info=array(
								'link_pr'			=> $pr,
								'link_home_pr'		=> $home_pr,
								'link_isexist' 		=> 1,
								'link_isinfo'		=> 1,					
								'link_lastupdate'	=> date('y-m-d H:i:s')
								);
					$plmlink->update($info,'link_id='.$id);
					}
					else{
						$_pr = new Http_Google_Pr($key);
						$home_pr=$_pr->getHomePR();
						$info=array(
								'link_pr'			=> 0,
								'link_home_pr'		=> $home_pr,
								'link_isexist' 		=> $link_isexist,
								'link_isinfo'		=> 1,					
								'link_lastupdate'	=> date('y-m-d H:i:s')
								);
						$plmlink->update($info,'link_id='.$id);
						}					
				}	
				success("ok");
			break;
			case "checkkeyword":
				Zend_loader::loadclass("keyword");
				Zend_Loader::loadClass('Mivec_Http_Socket');
				Zend_Loader::loadClass('Http_Google_Data');
				Zend_Loader::loadClass('Http_Google_Pr');
				$keyword=new keyword();
				foreach($_array as $id){
					$_temp=$keyword->fetchRow('keyword_id='.$id);
					$key=$_temp['keyword_link'];
					$_client = new Mivec_Http_Socket($key);
					$_status=$_client->request()->getStatus();
					$link_isexist=($_status=="200")?"1":"0";
					$normal = new Http_Google_Data($key);
					$_resultCount=$normal->getResultCount();
					if(_resultCount>0){
					$_pr = new Http_Google_Pr($key);	
					$pr=$_pr->getPR();
					$home_pr=$_pr->getHomePR();
					$info=array(
								'keyword_pr'			=> $pr,
								'keyword_homepr'		=> $home_pr,
								'link_isexist' 			=> 1,
								'keyword_isinfo'		=> 1								
								);
					$keyword->update($info,'keyword_id='.$id);
					}
					else{
						$_pr = new Http_Google_Pr($key);
						$home_pr=$_pr->getHomePR();
						$info=array(
								'keyword_pr'			=> 0,
								'keyword_homepr'		=> $home_pr,
								'keyword_isexist' 		=> $link_isexist,
								'keyword_isinfo'		=> 1								
								);
						$keyword->update($info,'keyword_id='.$id);
						}					
				}	
				success("ok");
			break;
			default:
				echo "类型错误";
			break;
		}
		exit;
	}
	
	
	function testAction(){
		exit;
		$id='8002';
		//将在线数据库的数据连接全部导入到数据表中		
		Zend_Loader::loadClass('product');
		Zend_Loader::loadclass('plmlink');
		$plmlink=new plmlink();
		$product=new product($id);
		$web_url="http://".$product->web_url;
		$products=$product->getAll();
		foreach($products as $product){
			//print_r($product);exit;
			$info=array(
								'link_linktype_id'			=> 6,
								'link_client_id'			=> 9,
								'link_site_id'				=> 20,
								'link_address'				=> $web_url."/".$product['url_path'],
								'link_input_date'			=> date('y-m-d H:i:s')
								);			
			$plmlink->insert($info);
			}		
		exit;		
	}
	
	
	function chartdataAction(){
		$type=$this->_request->getParam('type');
		$where=$this->_request->getParam("where");
		switch($type){
		case 'column':
		$select=$this->select;
		$select->from('plm_link',"link_user_id,DATE_FORMAT(link_input_date,'%y/%m') as month,count(*) as count");		
		$select->join('admin_user','plm_link.link_user_id=admin_user.u_id','username');	
		if(!empty($where)){
			$select->where(stripslashes($where));
		}
		$select->group("DATE_FORMAT(link_input_date,'%y/%m')");		
		$total=sizeof($this->db->fetchAll($select->__toString()));
		$data=$this->db->fetchAll($select->__toString());
		result($total,$data);
		break;
		case 'line':
		$select=$this->select;
		$select->from('plm_link',"link_user_id,DATE_FORMAT(link_input_date,'%m/%d') as day,count(*) as count");		
		$select->join('admin_user','plm_link.link_user_id=admin_user.u_id','username');	
		if(!empty($where)){
			$select->where(stripslashes($where));
		}
		$select->group("DATE_FORMAT(link_input_date,'%m-%d')");		
		$total=sizeof($this->db->fetchAll($select->__toString()));
		$data=$this->db->fetchAll($select->__toString());
		result($total,$data);	
		break;
		default:	
		$select=$this->select;
		$select->from('plm_link',"count(*) as count");
		$select->join('admin_user','plm_link.link_user_id=admin_user.u_id','username');	
		if(!empty($where)){
			$select->where(stripslashes($where));
		}
		$select->group('link_user_id');
		$total=sizeof($this->db->fetchAll($select->__toString()));
		$select->limit($limit,$start);
		$data=$this->db->fetchAll($select->__toString());		
		result($total,$data);
		}
		exit;
		}
	
	function tongjiAction() {
	//statements
	}
	
	function __call($method,$args){	
		echo $method;exit;
		if ('Action' == substr($method, -6)) 
    	{            
    		// If the action method was not found, forward to the index action            
    		return $this->_forward('index');        
    	}        
    	// all other methods throw an exception        
    	throw new Exception('Invalid method "' . $method . '" called');    
		}
}
?>